home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 4 / Meeting Pearls Vol. IV (1996)(GTI - Schatztruhe)[!].iso / Pearls / dev / C-Src / HexDump / HexDump.c < prev    next >
C/C++ Source or Header  |  1996-06-02  |  3KB  |  185 lines

  1. /*
  2. **    HexDump
  3. **
  4. **        © 1996 by Timo C. Nentwig
  5. **        All Rights Reserved !
  6. **
  7. **        Tcn@techbase.in-berlin.de
  8. **
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16.  
  17. #include <pragmas/exec_pragmas.h>
  18. #include <pragmas/dos_pragmas.h>
  19.  
  20. #include <strings.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24. #define INVALID_CHAR    '.'
  25.  
  26. struct    AnchorPath   *ap;
  27. BPTR                    File;
  28.  
  29.     // Freeing resources if CTRL-C
  30.  
  31. #ifdef __SASC
  32.  
  33.     VOID __regargs
  34.     _CXBRK (VOID)
  35.     {
  36.  
  37.         if (File)              // close file
  38.             Close (File);
  39.  
  40.         if (ap)                // free anchorpath struct
  41.             FreeVec (ap);
  42.  
  43.         if (DOSBase)           // close dos.library
  44.             CloseLibrary ((struct Library *) DOSBase);
  45.  
  46.         exit (0);
  47.  
  48.     }
  49.  
  50. #endif
  51.  
  52.     // Main
  53.  
  54. VOID
  55. main (UWORD argc, STRPTR *argv)
  56. {
  57.  
  58.         // No args or '?'
  59.  
  60.     if (argc == 1 || *argv [1] == '?')
  61.     {
  62.  
  63.         printf ("USAGE: %s <file1> <file2> <...>\n", argv[0]);
  64.  
  65.     }
  66.     else
  67.     {
  68.  
  69.         #define    BUFFSIZE    240
  70.  
  71.             // Open dos.library
  72.  
  73.         if (DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library", LIBRARY_MINIMUM))
  74.         {
  75.  
  76.             LONG    err;
  77.  
  78.             if (ap = AllocVec (sizeof (struct AnchorPath) + BUFFSIZE, MEMF_CLEAR))
  79.             {
  80.  
  81.                 UWORD    cnt;
  82.                 LONG     number    =  1;
  83.  
  84.                     // Evaluate every file
  85.  
  86.                 for (cnt = 1; cnt < argc; cnt++)
  87.                 {
  88.  
  89.                     ap -> ap_Strlen = BUFFSIZE;
  90.  
  91.                         // Evaluate pattern
  92.  
  93.                     for (err = MatchFirst (argv [cnt], ap); err == 0; err = MatchNext (ap), number++)
  94.                     {
  95.  
  96.  
  97.                             // Open file
  98.  
  99.                         if (File = Open (ap -> ap_Buf, MODE_OLDFILE))
  100.                         {
  101.  
  102.                             UWORD    k;
  103.                             UBYTE    buff [16];
  104.                             UWORD    offset = 0;
  105.  
  106.                                 // Read a block of bytes
  107.  
  108.                             while ((k = Read (File, buff, 16)) > 0)
  109.                             {
  110.  
  111.                                 UWORD    j;
  112.  
  113.                                     // Print offset
  114.  
  115.                                 printf ("\033[1m%08lx:\033[22m  ", offset);
  116.  
  117.                                     // Hex dumping ...
  118.  
  119.                                 for (j = 0; j < k; j++)
  120.                                 {
  121.  
  122.                                     printf ("%02x", (UBYTE) buff [j]);
  123.  
  124.                                     if ( ! ((j + 1) % 4))
  125.                                         printf(" ");
  126.  
  127.                                 }
  128.  
  129.                                 for (j = 0; j < (17 - k); j++)
  130.                                 {
  131.  
  132.                                     printf ("  ");
  133.  
  134.                                     if ( ! ((j+1) % 4))
  135.                                         printf (" ");
  136.  
  137.                                 }
  138.  
  139.                                 for (j = 0; j < k; j++)
  140.                                     printf ("%c", ((buff [j] >= ' ') && (buff [j] <= 'z')) ? buff [j] : INVALID_CHAR);
  141.  
  142.                                     // 16 bytes worked up
  143.  
  144.                                 if (k == 16)
  145.                                     printf ("\n");
  146.                                 else if (k > 0)
  147.                                     printf("\n");
  148.  
  149.                                 offset += 4;
  150.  
  151.                             }
  152.  
  153.                                 // Close file
  154.  
  155.                             Close (File);
  156.  
  157.                         }
  158.                         else
  159.                         {
  160.  
  161.                             printf ("Couldn't access file \"%s\"\n", ap -> ap_Buf);
  162.  
  163.                         }
  164.  
  165.                             // Close dos.library
  166.  
  167.                         CloseLibrary ((struct Library *) DOSBase);
  168.  
  169.                     }
  170.  
  171.                 }
  172.  
  173.                     // Finished, free resources
  174.  
  175.                 MatchEnd (ap);
  176.                 FreeVec  (ap);
  177.  
  178.             }
  179.  
  180.         }
  181.  
  182.     }
  183.  
  184. }
  185.